#include "DXLightManager.h"
#include "GlobalDefines.h"

DXLightManager::DXLightManager()
{
	lightnumber=-1;
}

DXLightManager::~DXLightManager()
{
	if(lights)
	{
		delete lights;
	}
}

int DXLightManager::createPointLight( LPDIRECT3DDEVICE9 a_device,D3DXVECTOR3 a_position, 
						            float a_property, float a_attenuation, float a_range)
{
	D3DLIGHT9 light;
    light.Type         = D3DLIGHT_POINT;
    light.Diffuse.r    = light.Diffuse.g  = light.Diffuse.b  = a_property;
    light.Specular.r   = light.Specular.g = light.Specular.b = a_property;
    light.Ambient.r    = light.Ambient.g  = light.Ambient.b  = a_property;
    light.Position     = a_position;
    light.Attenuation0 = light.Attenuation1 = light.Attenuation2 = a_attenuation;
    light.Range        = a_range;
	newlight(a_device, light, true);
	return lightnumber;

}

int DXLightManager::createDirectLight(LPDIRECT3DDEVICE9 a_device, D3DXVECTOR3 a_position, D3DXVECTOR3 a_direction,
						            float a_property, float a_attenuation, float a_range)
{
	D3DLIGHT9 light;
    light.Type         = D3DLIGHT_DIRECTIONAL;
    light.Diffuse.r    = light.Diffuse.g  = light.Diffuse.b  = a_property;
    light.Specular.r   = light.Specular.g = light.Specular.b = a_property;
    light.Ambient.r    = light.Ambient.g  = light.Ambient.b  = a_property;
    light.Position     = a_position;
	D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &a_direction );
    light.Attenuation0 = light.Attenuation1 = light.Attenuation2 = a_attenuation;
    light.Range        = a_range;
	newlight(a_device, light, true);
	return lightnumber;

}

int DXLightManager::createSpotLight(LPDIRECT3DDEVICE9 a_device,D3DXVECTOR3 a_position,D3DXVECTOR3 a_direction,
									 float a_degrees,float a_property,float a_attenuation, float a_range)
{
	D3DLIGHT9 light;
    light.Type         = D3DLIGHT_SPOT;
    light.Diffuse.r    = light.Diffuse.g  = light.Diffuse.b  = a_property;
    light.Specular.r   = light.Specular.g = light.Specular.b = a_property;
    light.Ambient.r    = light.Ambient.g  = light.Ambient.b  = a_property;
    light.Position     = a_position;
	D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &a_direction );
	light.Theta        = a_degrees;
    light.Attenuation0 = light.Attenuation1 = light.Attenuation2 = a_attenuation;
    light.Range        = a_range;
	newlight(a_device,light, true);
	return lightnumber;
}

void DXLightManager::updateDiffuseRGB(int id, float arr, float gee, float bee)
{
	lights[id].Diffuse.r  = arr;
	lights[id].Diffuse.g  = gee;
	lights[id].Diffuse.b  = bee;
}
void DXLightManager::updateSpecularRGB(int id, float arr, float gee, float bee)
{
	lights[id].Specular.r  = arr;
	lights[id].Specular.g  = gee;
	lights[id].Specular.b  = bee;
}
void DXLightManager::updateAmbientRGB(int id, float arr, float gee, float bee)
{
	lights[id].Ambient.r  = arr;
	lights[id].Ambient.g  = gee;
	lights[id].Ambient.b  = bee;
}
void DXLightManager::updateAttenuation012(int id, float a_first, float a_second, float a_third)
{
	lights[id].Attenuation0 = a_first;
	lights[id].Attenuation1 = a_second;
	lights[id].Attenuation2 = a_third;
}
void DXLightManager::updateRange(int id, float a_range)
{
	lights[id].Range = a_range;
}
void DXLightManager::updateDegrees(int id, float a_degrees)
{
	lights[id].Theta = a_degrees;
}
void DXLightManager::updateActivity(int id,LPDIRECT3DDEVICE9 device, bool a_on_off)
{
	device->LightEnable(id, a_on_off );
}
void DXLightManager::updatePosition(int id, D3DXVECTOR3 a_position)
{
	lights[id].Position=a_position;
}
void DXLightManager::updateDirection(int id, D3DXVECTOR3 a_direction)
{
	D3DXVec3Normalize( (D3DXVECTOR3*)&lights[id].Direction, &a_direction );
}

void DXLightManager::newlight(LPDIRECT3DDEVICE9 a_device, D3DLIGHT9 a_light, bool active)
{
	lightnumber++;
	D3DLIGHT9* newlightlist;
	newlightlist = new D3DLIGHT9[lightnumber+1]; 
	for(int i=0;i<lightnumber;++i)
	{
		newlightlist[i] = lights[i];
		a_device->SetLight(i, &newlightlist[i] );
	}
	newlightlist[lightnumber]=a_light;
	lights = newlightlist;
	//delete newlightlist;
	a_device->SetLight(lightnumber, &lights[lightnumber] );
    a_device->LightEnable(lightnumber, active );
}